<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="博文标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入博文标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="发布者名称" prop="pushId">
        <el-input v-model="queryParams." placeholder="请输入发布者" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="发布时间" prop="pushTime">
        <el-date-picker clearable v-model="queryParams.pushTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-input v-model="queryParams.isTop" placeholder="请输入是否置顶" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="浏览量" prop="viewCount">
        <el-input v-model="queryParams.viewCount" placeholder="请输入浏览量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否允许评论" prop="isComment">
        <el-input v-model="queryParams.isComment" placeholder="请输入是否允许评论" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['blog:blog:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['blog:blog:edit']">修改
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['blog:blog:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['blog:blog:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文章id" align="center" prop="id" />
      <el-table-column label="博文标题" align="center" prop="title" />
      <el-table-column label="发布者" align="center" prop="pushName" />
      <el-table-column label="文章图片" align="center" prop="imagrUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imagrUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" show-overflow-tooltip label="文章内容" align="center" prop="content" />
      <el-table-column width="200px" show-overflow-tooltip label="文章摘要" align="center" prop="summary" />
      <el-table-column label="是否置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          <!--<dict-tag :options="dict.type.is_top" :value="scope.row.isTop"/>-->
          <el-tag type="success" v-show="scope.row.isTop === '0'">置顶中</el-tag>
          <el-tag type="warning" v-show="scope.row.isTop === '1'">未置顶</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <!-- <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status" /> -->
          <el-tag type="success" v-show="scope.row.status === '0'">通过</el-tag>
          <el-tag type="warning" v-show="scope.row.status === '1'">未审核</el-tag>
          <el-tag type="danger" v-show="scope.row.status === '2'">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="viewCount" />
      <el-table-column label="是否允许评论" align="center" prop="isComment">
        <template slot-scope="scope">
          <el-tag type="success" v-show="scope.row.isComment === '0'">允许评论</el-tag>
          <el-tag type="warning" v-show="scope.row.isComment === '1'">不允许评论</el-tag>
        </template>
      </el-table-column>
      <!--<el-table-column label="逻辑删除" align="center" prop="isDel"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:blog:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:blog:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改文章管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form disabled ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="博文标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入博文标题" />
        </el-form-item>
        <el-form-item label="发布者" prop="pushId">
          <el-input v-model="form.pushName" placeholder="请输入发布者" />
        </el-form-item>
        <el-form-item label="文章图片" prop="imagrUrl">
          <image-upload v-model="form.imagrUrl" />
        </el-form-item>
        <el-form-item label="发布时间" prop="pushTime">
          <el-date-picker clearable v-model="form.pushTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文章内容">
          <editor readOnly v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="文章摘要" prop="summary">
          <el-input v-model="form.summary" placeholder="请输入文章摘要" />
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-select v-model="form.isTop" placeholder="请选择是否置顶">
            <!-- <el-option v-for="dict in dict.type.is_top" :key="dict.value" :label="dict.label" :value="dict.value"></el-option> -->
            <el-option value="0" label="已置顶">已置顶</el-option>
            <el-option value="1" label="未置顶">未置顶</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <!-- <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option> -->
            <el-option value="0" label="通过">通过</el-option>
            <el-option value="1" label="未审核">未审核</el-option>
            <el-option value="2" label="驳回">驳回</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="浏览量" prop="viewCount">
          <el-input v-model="form.viewCount" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="是否允许评论" prop="isComment">
          <el-select v-model="form.isComment" placeholder="请选择是否允许评论">
            <!-- <el-option v-for="dict in dict.type.is_comment" :key="dict.value" :label="dict.label" :value="dict.value"></el-option> -->
            <el-option value="0" label="允许">允许</el-option>
            <el-option value="1" label="不允许">不允许</el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="逻辑删除" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入逻辑删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitFormSuccess">审核通过</el-button>
        <el-button type="warning" @click="submitFormWarning">驳回文章</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  -->
    <!-- 审核记录框 预备发送前台用户消息 -->
    <el-dialog title="请填写审核结果信息" :visible.sync="openMessage" width="600px" append-to-body>
      <el-form ref="message" label-width="80px">
        <el-form-item label="初说明" prop="message">
          <el-input v-model="messageForm.message" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="记录标题" prop="title">
          <el-input v-model="messageForm.title" placeholder="请输入记录标题" />
        </el-form-item>
        <el-form-item label="具体原因" prop="tangible">
          <editor v-model="messageForm.tangible" :min-height="250" />
        </el-form-item>
        <el-form-item label="发送时间" prop="sendTime">
          <el-date-picker clearable v-model="messageForm.sendTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择发送时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBlog, getBlog, delBlog, addBlog, updateBlog, checkContest, sendMessage } from "@/api/blog/blogs/blog";

export default {
  name: "Blog",
  dicts: ['sys_common_status'],
  data() {
    return {
      messageForm: {
        message: null,
        title: null,
        tangible: null,
        sendTime: null,
        userId: null,
        isDel: '0',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章管理表格数据
      blogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        pushId: null,
        imagrUrl: null,
        pushTime: null,
        content: null,
        summary: null,
        isTop: null,
        status: null,
        viewCount: null,
        isComment: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      openMessage: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async submitFormSuccess() {
      const contesId = this.form.id
      const status = "0"
      const { code } = await checkContest(contesId, status)
      if (code == 200) {
        this.$notify({
          title: '成功',
          message: '审核完成',
          type: 'success',
        });
        //打开审核记录框填写审核信息
        this.openMessageModel()
      }
    },
    async submitFormWarning() {
      const contesId = this.form.id
      const status = "2"
      const { code } = await checkContest(contesId, status)
      if (code == 200) {
        this.$notify({
          title: '成功',
          message: '审核完成',
          type: 'success',
          offset: 100
        });
        //打开审核记录框填写审核信息
        this.openMessageModel()
      }
    },
    openMessageModel() {
      //开启
      this.openMessage = true
    },
    /** 查询文章管理列表 */
    getList() {
      this.loading = true;
      listBlog(this.queryParams).then(response => {
        this.blogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        pushId: null,
        imagrUrl: null,
        pushTime: null,
        content: null,
        summary: null,
        isTop: null,
        status: null,
        viewCount: null,
        isComment: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBlog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章管理";
      });
    },
    /** 提交按钮 */
    async submitForm() {
      //赋值用户id
      this.messageForm.userId = this.form.pushId
      //调用接口保存记录
      const { code } = await sendMessage(this.messageForm)
      if (code === 200) {
        this.$notify({
          title: '成功',
          message: '审核流程结束',
          type: 'success',
        });
        this.openMessage = false
        this.open = false
        this.getList()
        this.messageForm = {}
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章管理编号为"' + ids + '"的数据项？').then(function () {
        return delBlog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blog/blog/export', {
        ...this.queryParams
      }, `blog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
